package com.woniuxy.caradmin.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.caradmin.entity.pojo.Vehiclefiles;
import com.woniuxy.caradmin.entity.vo.VehiclefilesVo;
import com.woniuxy.caradmin.exception.CarException;
import com.woniuxy.caradmin.service.VehiclefilesService;
import com.woniuxy.caradmin.util.Constaint;
import com.woniuxy.caradmin.util.DataGridView;
import com.woniuxy.caradmin.util.R;
import com.woniuxy.caradmin.util.ResultCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author womenzu
 * @since 2020-12-07
 */
@RestController
@RequestMapping("/vehiclefiles")
public class VehiclefilesController {
    @Autowired
    private VehiclefilesService vehiclefilesService;
    @ApiOperation(value = "获取所有的车辆信息")
    @GetMapping(value = "/selectVehicleAll")
    public R selectVehicleAll(){
        List<Vehiclefiles> vehiclefilesList = vehiclefilesService.selectVehicleAll();
        if(vehiclefilesList!=null&&vehiclefilesList.size()>0){
            return R.ok().code(ResultCode.SUCCESS).message(Constaint.SUCCESS).data("vehiclefilesList",vehiclefilesList);
        }else{
            throw new CarException(ResultCode.VEHICLELISTNULL, Constaint.VEHICLELISTNULL);
        }
    }
    @ApiOperation(value = "根据id查询车辆信息")
    @GetMapping(value = "/selecctVehicleById")
    public R selecctVehicleById(Integer vehid){
        Vehiclefiles vehiclefile = vehiclefilesService.selecctVehicleById(vehid);
        if (vehiclefile != null) {
            Date purchasedate = vehiclefile.getPurchasedate();
            java.sql.Date purchasedatesql=new java.sql.Date(purchasedate.getTime());
            vehiclefile.setPurchasedate(purchasedatesql);
            return R.ok().code(ResultCode.SUCCESS).message(Constaint.SUCCESS).data("vehiclefile",vehiclefile);
        }else{
            throw new CarException(ResultCode.VEHICLEISNULL,Constaint.VEHICLEISNULL);
        }
    }
    @ApiOperation(value = "增加车辆")
    @PostMapping(value = "/addVehicle")
    public R addVehicle(HttpServletRequest request){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String vehiclenum=request.getParameter("vehiclenum");
        String brandid=request.getParameter("brand");
        String clazzid=request.getParameter("clazz");
        String typeid=request.getParameter("type");
        String colorid=request.getParameter("color");
        String seat=request.getParameter("seat");
        String did=request.getParameter("did");
        String purchasedate=request.getParameter("purchasedate");
        String carstatusid=request.getParameter("carstatus");
        String available=request.getParameter("available");
        Date purchasedateutil=null;
        try {
            purchasedateutil=sdf.parse(purchasedate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        java.sql.Date purchasedatesql=new java.sql.Date(purchasedateutil.getTime());
        Vehiclefiles vehiclefilesAdd = new Vehiclefiles();
        vehiclefilesAdd.setVehiclenum(vehiclenum);
        vehiclefilesAdd.setBrandid(Integer.parseInt(brandid));
        vehiclefilesAdd.setClazzid(Integer.parseInt(clazzid));
        vehiclefilesAdd.setTypeid(Integer.parseInt(typeid));
        vehiclefilesAdd.setColorid(Integer.parseInt(colorid));
        vehiclefilesAdd.setSeat(seat);
        vehiclefilesAdd.setDid(Integer.parseInt(did));
        vehiclefilesAdd.setPurchasedate(purchasedatesql);
        vehiclefilesAdd.setStatusid(Integer.parseInt(carstatusid));
        vehiclefilesAdd.setAvailable(Integer.parseInt(available));
        boolean flag = vehiclefilesService.addVehiclefiles(vehiclefilesAdd);
        if(flag){
            return R.ok().code(ResultCode.SUCCESS).message(Constaint.SUCCESS);
        }else{
            throw new CarException(ResultCode.VEHICLEADDFAIL,Constaint.VEHICLEADDFAIL);
        }
    }
    @ApiOperation(value = "批量软删除车辆")
    @PostMapping(value = "/removeVehiclefiles")
    public R removeVehiclefiles(Integer[] ids){
        boolean flag = vehiclefilesService.removeVehiclefiles(ids);
        if(flag){
            return R.ok().code(ResultCode.SUCCESS).message(Constaint.SUCCESS);
        }else{
            throw new CarException(ResultCode.VEHICLEREMOVEFAIL,Constaint.VEHICLEREMOVEFAIL);
        }
    }
    @ApiOperation(value = "查询可用车辆信息")
    @GetMapping(value = "/selectVehiclefilesByUseble")
    public R selectVehiclefilesByUseble(){
        List<Vehiclefiles> vehiclefilesUseble = vehiclefilesService.selectVehiclefilesByUseble();
        if (vehiclefilesUseble != null) {
            return R.ok().code(ResultCode.SUCCESS).message(Constaint.SUCCESS).data("vehiclefilesUseble",vehiclefilesUseble);
        }else{
            throw new CarException(ResultCode.VEHICLEUSEBLENULL,Constaint.VEHICLEUSEBLENULL);
        }
    }
    @ApiOperation(value = "更新车辆信息")
    @GetMapping(value = "/updateVehiclefiles")
    public R updateVehiclefiles(HttpServletRequest request){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String id=request.getParameter("id");
        String vehiclenum=request.getParameter("vehiclenum");
        String brandid=request.getParameter("brand");
        String clazzid=request.getParameter("clazz");
        String typeid=request.getParameter("type");
        String colorid=request.getParameter("color");
        String seat=request.getParameter("seat");
        String did=request.getParameter("did");
        String purchasedate=request.getParameter("purchasedate");
        String carstatusid=request.getParameter("carstatus");
        String available=request.getParameter("available");
        Date purchasedateutil=null;
        try {
            purchasedateutil=sdf.parse(purchasedate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        java.sql.Date purchasedatesql=new java.sql.Date(purchasedateutil.getTime());
        Vehiclefiles vehiclefilesUpd = new Vehiclefiles();
        vehiclefilesUpd.setId(Integer.parseInt(id));
        vehiclefilesUpd.setVehiclenum(vehiclenum);
        vehiclefilesUpd.setBrandid(Integer.parseInt(brandid));
        vehiclefilesUpd.setClazzid(Integer.parseInt(clazzid));
        vehiclefilesUpd.setTypeid(Integer.parseInt(typeid));
        vehiclefilesUpd.setColorid(Integer.parseInt(colorid));
        vehiclefilesUpd.setSeat(seat);
        vehiclefilesUpd.setDid(Integer.parseInt(did));
        vehiclefilesUpd.setPurchasedate(purchasedatesql);
        vehiclefilesUpd.setStatusid(Integer.parseInt(carstatusid));
        vehiclefilesUpd.setAvailable(Integer.parseInt(available));
        boolean flag = vehiclefilesService.updateVehiclefiles(vehiclefilesUpd);
        if (flag) {
            return R.ok().code(ResultCode.SUCCESS).message(Constaint.SUCCESS);
        }else{
            throw new CarException(ResultCode.VEHICLEUPDFAIL,Constaint.VEHICLEUPDFAIL);
        }
    }
    @ApiOperation(value = "车辆信息分页")
    @GetMapping(value = "/getVehiclefilesList")
    public DataGridView getVehiclefilesList(VehiclefilesVo vehiclefilesVo){
        Page<Object> page = PageHelper.startPage(vehiclefilesVo.getPage(), vehiclefilesVo.getLimit());
        List<Vehiclefiles> vehiclefilesList = vehiclefilesService.selectVehicleByPage(vehiclefilesVo);
        if(vehiclefilesList!=null&&vehiclefilesList.size()>0){
            return new DataGridView(page.getTotal(),vehiclefilesList);
        }else{
            throw new CarException(ResultCode.ERROR,Constaint.ERROR);
        }
    }
    @ApiOperation(value = "根据id软删除车辆")
    @GetMapping(value = "/removeVehicleById")
    public R removeVehicleById(Integer id){
        boolean flag = vehiclefilesService.removeVehicleById(id);
        if(flag){
            return R.ok().code(ResultCode.SUCCESS).message(Constaint.SUCCESS);
        }else{
            throw new CarException(ResultCode.VEHICLEREMOVEFAIL,Constaint.VEHICLEREMOVEFAIL);
        }
    }
    @ApiOperation(value = "根据id更新车辆照片")
    @PostMapping(value = "/updateImgById")
    public R updateImgById(Integer id,
                           Vehiclefiles vehiclefiles,
                           HttpServletRequest request,
                           MultipartFile carimg){
        System.out.println("id:"+id);
        vehiclefiles.setId(id);
        String realPath = request.getServletContext().getRealPath("/images/cars/");
        System.out.println("realPath:"+realPath);
        System.out.println("carimg:"+carimg);
        /*String fileName=carimg.getOriginalFilename();
        String fileSuffix=fileName.substring(fileName.lastIndexOf("."));
        String fileNewName= UUID.randomUUID().toString();
        String filePath=realPath+fileNewName+fileSuffix;
        try {
            carimg.transferTo(new File(filePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
        vehiclefiles.setFrontimg("http://localhost:8080/images/cars/"+fileNewName+fileSuffix);
        boolean flag = vehiclefilesService.updateVehiclefilesImg(vehiclefiles);
        if(flag){
            return R.ok().code(ResultCode.SUCCESS).message(Constaint.SUCCESS);
        }else{
            throw new CarException(ResultCode.VEHICLEREMOVEFAIL,Constaint.VEHICLEREMOVEFAIL);
        }*/
        return null;
    }
}

